/*
 *    Copyright (c) 2018-2025, fanxl All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: fanxl (xunlian_fan001@126.com)
 */

package com.taiji.dicom4cloud.smart.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taiji.dicom4cloud.common.core.utils.R;
import com.taiji.dicom4cloud.common.log.annotation.SnowLog;
import com.taiji.dicom4cloud.smart.model.entity.CarOwer;
import com.taiji.dicom4cloud.smart.model.entity.RfidEquipment;
import com.taiji.dicom4cloud.smart.service.RfidEquipmentService;
import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author fanxl
 * @date 2021-07-06 10:45:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rfidequipment" )
@Api(value = "rfidequipment", tags = "管理")
public class RfidEquipmentController {

    private final RfidEquipmentService rfidEquipmentService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param rfidEquipment 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getRfidEquipmentPage(Page page, RfidEquipment rfidEquipment) {
        return R.ok(rfidEquipmentService.page(page, Wrappers.<RfidEquipment>lambdaQuery()
                .eq(StringUtil.isNotEmpty(rfidEquipment.getOrgId())&&!rfidEquipment.getOrgId().equals("-1"),
                        RfidEquipment::getOrgId,rfidEquipment.getOrgId())
                .like(StringUtil.isNotEmpty(rfidEquipment.getRfidEquipmentName()),RfidEquipment::getRfidEquipmentName,rfidEquipment.getRfidEquipmentName())
                .like(StringUtil.isNotEmpty(rfidEquipment.getRfidEquipmentCode()),RfidEquipment::getRfidEquipmentCode,rfidEquipment.getRfidEquipmentCode())));
    }

    @ApiOperation(value = "查询所有设备列表", notes = "查询所有设备列表")
    @GetMapping("/all" )
    public R getRfidEquipmentAll(String orgId){
        return R.ok(rfidEquipmentService.list(
                Wrappers.<RfidEquipment>lambdaQuery()
                        .eq(StringUtil.isNotEmpty(orgId)&&!orgId.equals("-1"),
                                RfidEquipment::getOrgId,orgId)));
    }


    /**
     * 通过id查询
     * @param rfid id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{rfid}" )
    public R getById(@PathVariable("rfid" ) String rfid) {
        return R.ok(rfidEquipmentService.getById(rfid));
    }

    /**
     * 新增
     * @param rfidEquipment 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SnowLog("新增" )
    @PostMapping
    public R save(@RequestBody RfidEquipment rfidEquipment) {
        rfidEquipment.setRfidEquipmentId(CommonUtils.getUUID());
        rfidEquipment.setDelFlag("0");
        return R.ok(rfidEquipmentService.save(rfidEquipment));
    }

    /**
     * 修改
     * @param rfidEquipment 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SnowLog("修改" )
    @PutMapping
    public R updateById(@RequestBody RfidEquipment rfidEquipment) {
        return R.ok(rfidEquipmentService.updateById(rfidEquipment));
    }

    /**
     * 通过id删除
     * @param rfid id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SnowLog("通过id删除" )
    @DeleteMapping("/{rfid}" )
    public R removeById(@PathVariable String rfid) {
        return R.ok(rfidEquipmentService.removeById(rfid));
    }


    @ApiOperation(value = "设置功率", notes = "设置功率")
    @PostMapping("/bindRfidPower")
    public R bindRfidPower(@RequestBody RfidEquipment rfidEquipment){
        RfidEquipment rfidEquipment1 = rfidEquipmentService.getById(rfidEquipment.getRfidEquipmentId());
        if(ObjectUtil.isNotNull(rfidEquipment1)){
            rfidEquipment1.setRfidPower(rfidEquipment.getRfidPower());
            rfidEquipmentService.updateById(rfidEquipment1);
        }
        return R.ok(Boolean.TRUE);
    }
}
